# 导入neo4j官方驱动包
from neo4j import GraphDatabase

# 用户、电影和评分数据
users = ["张三", "李四", "王五"]
movies = ["盗梦空间", "星际穿越", "黑客帝国"]
ratings = [
    #    ("张三", "盗梦空间", 5),
    #    ("张三", "黑客帝国", 4),
    #    ("李四", "盗梦空间", 4),
    #    ("李四", "星际穿越", 5),
    #    ("王五", "黑客帝国", 5),
    ("王五", "盗梦空间", 5),
    ("王五", "星际穿越", 5),
]

uri = "bolt://localhost:7687"
auth = ("neo4j", "12345678")
driver = GraphDatabase.driver(uri, auth=auth)
# CREATE (u:User {name:"张三"})
# CREATE (b:Book {name:"活着"})
# CREATE CONSTRAINT unique_book_name FOR (u:Book) REQUIRE u.name IS UNIQUE
with driver.session() as session:
    for name in users:
        session.run("MERGE (u:User {name:$name}) ", {"name": name})
    for title in movies:
        session.run("MERGE (m:Movie {title:$title}) ", {"title": title})
    for u_name, m_title, score in ratings:
        session.run(
            "MATCH (u:User {name:$u_name}),(m:Movie {title:$m_title})"
            "MERGE (u)-[:RATED {score:$score}]->(m)",
            {"u_name": u_name, "m_title": m_title, "score": score},
        )
driver.close()
